﻿using System;
using System.Linq;
using SampleApp.Domain;
using SampleApp.Data;
using System.Collections.Generic;

namespace SampleApp.Service
{
    public class SysDeptService : ISysDeptService
    {
        private ISysDeptRepository sysDeptRepository;
        private IRuntimeService runtimeService;
        private IDataCacheService dataCacheService;
        public SysDeptService(IRuntimeService runtimeService, IDataCacheService dataCacheService,ISysDeptRepository sysDeptRepository)
        {
            this.runtimeService = runtimeService;
            this.sysDeptRepository = sysDeptRepository;
            this.dataCacheService = dataCacheService;
        }

        public List<SysDept> GetDeptListFromDatabase()
        {
            var q = this.sysDeptRepository.GetMany(i =>true);
            return q.ToList();
        }


        public List<SysDept> GetSysDeptList()
        {
            return this.dataCacheService.DeptList;
        }


        public SysDept GetSysDeptByCode(string deptCode)
        {
            return this.sysDeptRepository.GetByID(deptCode);
        }

        public void CreateSysDept(SysDept sysDept)
        {
            this.sysDeptRepository.Add(sysDept);
            this.runtimeService.UnitOfWork.Commit();
        }


        public void DeleteSysDept(string deptCode)
        {
            var existsDept = this.GetSysDeptByCode(deptCode);
            this.sysDeptRepository.Delete(existsDept);
            this.runtimeService.UnitOfWork.Commit();
        }

        public void UpdateSysDept(SysDept sysDept)
        {
            var exists = this.GetSysDeptByCode(sysDept.DeptCode);
            this.sysDeptRepository.SetValues(sysDept, exists);
            this.runtimeService.UnitOfWork.Commit();
        }

        
    }
}
